<?php

class multitender_action_find_users extends multitender_action {

    function  __construct() {
        parent::__construct();
        $rights = $this->conf['user']['right'];
        if ($rights<=100) {
            $tpl = $this->new_tpl();
            $tpl->display("msg_right.tpl");
            exit;
        }
    }

    function run() {
        $tpl=$this->new_tpl();

        $link_base = $this->conf['pref']['link_base'];
        $link_act = "action=find_users";
        $search = @ $_REQUEST['search'] ? $_REQUEST['search'] : false;
        $page = @ $_GET['page'] ? $_GET['page'] : 1;
        $total = 0;
        $rectopg = 25;

        $m_fu = $this->new_model('find_users');

        $message = null;
        $error = false;

        if ($search) {
            $tpl->assign('search', $search);
            if (mb_strlen($search, "utf-8") < 3) {
                $message = "Слишком короткий поиск";
                $error = true;
            } else {
                if (!$s_ids = $m_fu->get_tender_users($search)) {
                    $s_ids = array();
                }

                if ($id = $m_fu->get_joomla_user($search)) {
                    $s_ids[] = $id;
                }
            }
        } else {
            $search=null;
            $s_ids = null;
        }

        if (!$error) {
            if ($users = $m_fu->get_joomla_users($search, $s_ids, ($page-1)*$rectopg, $rectopg)) {
                $total = $users['total'];
                unset($users['total']);

                $j_ids = array();
                foreach ($users as &$user) {
                    $user['sys_id'] = false;
                    $j_ids[] = $user['id'];
                }

                if ($t_users = $m_fu->get_ids_users($j_ids)) {
                    $s_ids=array();
                    foreach ($users as &$user) {
                        foreach ($t_users as $t_user) {
                            if ($t_user['joomla_id']==$user['id']) {
                                $user['sys_id'] = $t_user['Id'];
                                $t_ids[] = $t_user['Id'];
                            }
                        }
                    }
                    if ($firms = $m_fu->get_firms($t_ids)) {
                        $firm_obj = $this->new_model('firm');
                        foreach ($users as &$user) {
                            foreach ($firms as $firm) {
                                if ($firm['user_id']==$user['sys_id']) {
                                    $user['firm'] = $firm_obj->prepare_firm($firm);;
                                }
                            }
                        }
                    }
                }

                // Подсветка
                if ($search) {
                    foreach ($users as &$user) {
                        foreach ($user as $key => &$data) {
                            if ($key<>'sys_id' && $key<>'id') {
                                $data = preg_replace("/$search/iu", "<font color=#cc0000>\\0</font>" , $data);
                            } else {
                                $data = preg_replace("/^$search$/ium", "<font color=#cc0000>\\0</font>" , $data);
                            }
                        }
                    }
                }
                

                $tpl->assign('users', $users);
            } else {
                $message = "Не одного пользователя по запросу не найден";
            }
        }


        $tpl->assign('message', @ $message);

        $tpl->assign("total", $total);

        $scrol = $this->new_action("scrol");
        $scrol->link = $link_base.$link_act."&search=".$search.$scrol->link;
        $scrol->total = Ceil($total/$rectopg);
        $tpl->assign('scrol', $scrol->run());

        $tpl->assign('link_base', $link_base);
        $tpl->assign('link_act', $link_act);

        return $tpl->fetch('find_users.tpl');
    }
}

